package com.woniuxy.service.controller;

import com.woniuxy.commons.entity.Order;
import com.woniuxy.commons.entity.ServiceItem;
import com.woniuxy.commons.util.ResponseResult;
import com.woniuxy.commons.util.ResultStatus;
import com.woniuxy.service.service.OrderService;
import com.woniuxy.service.service.ServiceItemService;
import com.woniuxy.service.utils.PageMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/service")
//@CrossOrigin
public class ServiceController {
    @Resource
    private ServiceItemService serviceItemService;
    @Resource
    private OrderService orderService;
    @RequestMapping("/all/{page}/{size}")
    public ResponseResult all(@PathVariable("page") int page,@PathVariable("size") int size){
        PageMessage all=serviceItemService.all(page,size);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setMsg("成功查找所有服务");
        responseResult.setCode(200);
        responseResult.setData(all);
        return responseResult;
    }
    @RequestMapping("/enableservice/{page}/{size}")
    public ResponseResult enableservice(@PathVariable("page") int page,@PathVariable("size") int size){
        PageMessage all=serviceItemService.enableservice(page,size);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setMsg("成功查找所有已启用服务");
        responseResult.setCode(200);
        responseResult.setData(all);
        return responseResult;
    }
    @RequestMapping("/disableservice/{page}/{size}")
    public ResponseResult disableservice(@PathVariable("page") int page,@PathVariable("size") int size){
        PageMessage all=serviceItemService.disableservice(page,size);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setMsg("成功查找所有已禁用服务");
        responseResult.setCode(200);
        responseResult.setData(all);
        return responseResult;
    }
    @RequestMapping("/find/{page}/{size}/{key}")
    public ResponseResult find(@PathVariable("page") int page,@PathVariable("size") int size,@PathVariable("key") String key){

        PageMessage pageMessage=serviceItemService.find(key,page,size);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setMsg("成功查找指定服务");
        responseResult.setCode(200);
        responseResult.setData(pageMessage);
        return responseResult;
    }
    @RequestMapping("/add")
    public ResponseResult add(@RequestBody ServiceItem serviceItem){
        boolean add= serviceItemService.add(serviceItem);
        ResponseResult responseResult=new ResponseResult();
        if(add){
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("成功添加服务");
            responseResult.setCode(200);
            responseResult.setData(null);
        }
        else{
            responseResult.setStatus(ResultStatus.FAIL);
            responseResult.setMsg("添加服务失败");
            responseResult.setCode(400);
            responseResult.setData(null);
        }
        return responseResult;
    }

    @RequestMapping("/del")
    public ResponseResult del(@RequestBody ServiceItem serviceItem){
        int serviceId=serviceItem.getServiceId();
        boolean del= serviceItemService.del(serviceId);
        ResponseResult responseResult=new ResponseResult();
        if(del){
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("删除服务成功");
            responseResult.setCode(200);
            responseResult.setData(null);
        }
        else{
            responseResult.setStatus(ResultStatus.FAIL);
            responseResult.setMsg("删除服务失败");
            responseResult.setCode(400);
            responseResult.setData(null);
        }
        return responseResult;
    }

    @RequestMapping("/update")
    public ResponseResult update(@RequestBody ServiceItem serviceItem){

        boolean update= serviceItemService.update(serviceItem);
        ResponseResult responseResult=new ResponseResult();
        if(update){
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("更改服务成功");
            responseResult.setCode(200);
            responseResult.setData(true);
        }
        else{
            responseResult.setStatus(ResultStatus.FAIL);
            responseResult.setMsg("更改服务失败");
            responseResult.setCode(400);
            responseResult.setData(null);
        }
        return responseResult;
    }
    @RequestMapping("/allenableservice")
    public ResponseResult allenableservice(){
        List<ServiceItem> all=serviceItemService.allenableservice();
        ResponseResult responseResult=new ResponseResult();
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setMsg("成功查找所有服务");
        responseResult.setCode(200);
        responseResult.setData(all);
        return responseResult;
    }
    @RequestMapping("/findenableservice/{key}")
    public ResponseResult findenableservice(@PathVariable("key")String key){
        List<ServiceItem> all=serviceItemService.findenableservice(key);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setMsg("成功查找所有服务");
        responseResult.setCode(200);
        responseResult.setData(all);
        return responseResult;
    }
    @RequestMapping("/buy")
    public ResponseResult buy(@RequestBody Order order){
        //再传一个
        boolean result=orderService.add(order);
        ResponseResult responseResult=new ResponseResult();
        if(result){
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("下单成功");
            responseResult.setCode(200);
            responseResult.setData(null);
        }
        else{
            responseResult.setStatus(ResultStatus.FAIL);
            responseResult.setMsg("下单失败");
            responseResult.setCode(400);
            responseResult.setData(null);
        }
        return responseResult;
    }

}
